﻿using JsonFx.Json;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class TextData
{
	private static TextData instance;

	public class ContentData
	{
		public string catid;
		public int id;
		public string text;
		public string text2;
	}

	public List<ContentData> text;

	private static TextData Inst { get { Init(); return instance; } }

	public static void Init()
	{
		if (instance == null)
		{
			var file = System.IO.File.Open(Application.persistentDataPath + "/text.json", System.IO.FileMode.Open);
			var bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

			instance = JsonReader.Deserialize<TextData>((string)bf.Deserialize(file));

			file.Close();
		}
	}

	public static string Get(string id)
	{
		return Localization.Get(id);
	}

	public static string Get(TextCat catid, int id)
	{
		try
		{
			return Inst.Text(catid.ToString(), id);
		}
		catch
		{
			return "[Error]";
		}
	}

	public static string Get2(TextCat catid, int id)
	{
		try
		{
			return Inst.Text2(catid.ToString(), id);
		}
		catch
		{
			return "[Error]";
		}
	}

	private string Text(string catid, int id)
	{
		return text.First(d => d.catid.Equals(catid) && d.id == id).text;
	}

	private string Text2(string catid, int id)
	{
		return text.First(d => d.catid.Equals(catid) && d.id == id).text2;
	}
}
